package com.service;

import com.entity.User;
import com.mapper.UserMapper;
import com.utils.JsonUtil;
import com.utils.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class UserService implements UserMapper {
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    JsonUtil jsonUtil = new JsonUtil();

    /**
     * 查找所有用户
     * @return User集合
     */
    @Override
    public List<User> findAllUser() {
        SqlSession sqlSession = factory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> allUser = mapper.findAllUser();
        return allUser;
    }

    /**
     * 根据用户名进行模糊查找
     * @param name
     * @return User集合
     */
    @Override
    public List<User> findUserByName(String name) {
        SqlSession sqlSession = factory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> UserByName = mapper.findUserByName(name);
        return UserByName;
    }

    /**
     * 根据用户ID查找用户
     * @param id
     * @return User对象
     */
    @Override
    public User findUserById(int id) {
        SqlSession sqlSession = factory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User UserById = mapper.findUserById(id);
        return UserById;
    }

    /**
     * 根据邮箱进行模糊查找
     * @param email
     * @return User集合
     */
    @Override
    public List<User> findUserByEmail(String email) {
        SqlSession sqlSession = factory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> UserByEmail = mapper.findUserByEmail(email);
        return UserByEmail;
    }

    /**
     * 实时查询该用户是否存在
     * @param name 用户名
     * @return 返回查询用户名结果
     * 查询结果为空，则返回null
     */
    @Override
    public User findUserName(String name) {
        SqlSession sqlSession = factory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User UserName = mapper.findUserName(name);

        return UserName;
    }

    /**
     * 查找用户总数
     * @return int 返回用户整数
     */
    @Override
    public int count() {
        SqlSession sqlSession = factory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int count = mapper.count();
        return count;
    }

    @Override
    public List<User> findUserPage(int pageNum, int offset) {
        int pageSize= (pageNum-1)*offset; //pageSize start条数
        SqlSession sqlSession = factory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> User = mapper.findUserPage(pageSize,offset);
        sqlSession.close();
        return User;
    }

    /**
     * 添加用户
     * @param User
     * @return 受影响的行数
     */
    @Override
    public int addUser(User User) {
        SqlSession sqlSession = factory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int rows = mapper.addUser(User);
        sqlSession.commit();
        return rows;
    }

    /**
     * 更新用户资料(不包括密码)
     * @param name 用户名
     * @param avatar 用户头像
     * @param email 用户邮箱
     * @param id 用户ID
     * @return 受影响的行数
     */
    @Override
    public int updateUserById(int id,String name,String email,String avatar,String description,String tel,String update_time) {
        SqlSession sqlSession = factory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int rows = mapper.updateUserById(id, name,email,avatar,description,tel,update_time);
        sqlSession.commit();
        return rows;
    }

    /**
     * 根据ID查询密码
     * @param id 用户id
     * @return 返回查询到的密码
     */
    @Override
    public String findPdById(int id) {
        SqlSession sqlSession = factory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        String pdById = mapper.findPdById(id);
        System.out.println("密码为："+pdById);
        return pdById;
    }

    /**
     * 根据ID修改用户密码
     * @param newPassword 用户新密码
     * @param id 用户ID
     * @return 受影响的行数
     */
    @Override
    public int updateUserPd(String newPassword, int id) {
        SqlSession sqlSession = factory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int rows = mapper.updateUserPd(newPassword, id);
        sqlSession.commit();
        return rows;
    }

    /**
     * 根据ID删除用户
     * @param id 用户ID
     * @return 受影响的行数
     */
    @Override
    public int delUserById(int id) {
        SqlSession sqlSession = factory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int b = mapper.delUserById(id);
        sqlSession.commit();
        return b;
    }

    public boolean editUserPd(int id,String oldPd,String newPd){
        String pd = findPdById(id);
        if (pd.equals(oldPd)){
            //相等
            int i = updateUserPd(newPd, id);
            System.out.println("密码已更新成功");
            return true;

        }else {
            System.out.println("密码更新失败");
            return false;
        }
    }
}
